package qdu.edu.cn.liu;

import qdu.edu.cn.liu.filter.GetHyperLink;
import qdu.edu.cn.liu.get.RequestURL;
import qdu.edu.cn.liu.read.ReadWordsFile;
import qdu.edu.cn.liu.store.StoreHyperlinkToRedisOrFile;

import java.io.BufferedWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;

public class Main {
    private static String inputFilePath = "/home/liu/words.csv";
    private static String outputFilePath = "/home/liu/hyperlinks.txt";

    public static void main(String[] args) throws IOException {
        StoreHyperlinkToRedisOrFile storeHyperlinkToRedisOrFile = new StoreHyperlinkToRedisOrFile();

        if (args != null) {
            if (args.length == 2) {
                inputFilePath = args[0];
                outputFilePath = args[1];
            }
        }

        String words;
        ReadWordsFile readWordsFile = new ReadWordsFile(inputFilePath);
        GetHyperLink getHyperLink = new GetHyperLink();
        RequestURL requestURL = new RequestURL();
        BufferedWriter bufferedWriter = storeHyperlinkToRedisOrFile.getBufferedWriter(outputFilePath);

        int count = 0;
        while ((words = readWordsFile.getWords()) != null) {
            String content = requestURL.getHtmlContent(words);

            getHyperLink.pattern(content, words);
            HashSet<String> hyperlinks = getHyperLink.getWordsAndHyperlinks().get(words);
            Iterator<String> iterator = hyperlinks.iterator();
            if (iterator.hasNext()) {
                bufferedWriter.write(words+" "+iterator.next());
                bufferedWriter.newLine();
                System.out.println(++count);
            }
        }
    }
}
